package com.mls.fivedistrict.controller.portal;

import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.UserAuth;
import com.mls.fivedistrict.server.UserAuthService;
import com.mls.fivedistrict.util.IdcardNoValidateUtil;
import com.mls.fivedistrict.util.jedis.RedisPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * Created by chenxiuxiang on 2018/4/1.
 */
@RestController
@RequestMapping("/api/app")
public class UserAuthController extends BaseController {
    @Autowired
    UserAuthService userAuthService;

    @PostMapping("/v1/userAuth")
    public ServiceResponse addUserAuth(@RequestBody UserAuth userAuth) {
        return ServiceResponse.createErrorResponse("请下载最新版APP进行认证");
//        Optional.ofNullable(userAuth.getRealName()).orElseThrow(() -> new CustomerException("真实姓名不存在"));
//        Optional.ofNullable(userAuth.getCradNo()).orElseThrow(() -> new CustomerException("身份证号不存在"));
//        Optional.ofNullable(userAuth.getPositiveImage()).orElseThrow(() -> new CustomerException("身份证正面不存在"));
//        Optional.ofNullable(userAuth.getReverseImage()).orElseThrow(() -> new CustomerException("手持身份证不存在"));
//        if (userAuth.getPositiveImage().equals("1") || userAuth.getPositiveImage().equals("3")) {
//            return ServiceResponse.createErrorResponse("请上传身份证正面照片");
//        }
//        if (userAuth.getReverseImage().equals("2") || userAuth.getReverseImage().equals("4")) {
//            return ServiceResponse.createErrorResponse("请上传身份手持照片");
//        }
//
//        if (!IdcardNoValidateUtil.is18ByteIdCard(userAuth.getCradNo())) {
//            return ServiceResponse.createErrorResponse("身份证格式不正确");
//        }
//        userAuth.setUserId(getUser().getId());
//        int row = userAuthService.userAuth(userAuth);
//        if (row > 0) {
//            return ServiceResponse.createSuccessResponse();
//        } else {
//            if (row == -2) {
//                RedisPoolUtil.del(getToken());
//                return ServiceResponse.createErrorResponse("用户被注销");
//            }
//            return ServiceResponse.createErrorResponse("认证失败");
//        }
    }


    @PostMapping("/v2/userAuth")
    public ServiceResponse userAuth(@RequestBody UserAuth userAuth) {
        Optional.ofNullable(userAuth.getRealName()).orElseThrow(() -> new CustomerException("真实姓名不存在"));
        Optional.ofNullable(userAuth.getCradNo()).orElseThrow(() -> new CustomerException("身份证号不存在"));
        Optional.ofNullable(userAuth.getPositiveImage()).orElseThrow(() -> new CustomerException("身份证正面不存在"));
        Optional.ofNullable(userAuth.getReverseImage()).orElseThrow(() -> new CustomerException("身份证国徽面不存在"));
        if (!IdcardNoValidateUtil.is18ByteIdCard(userAuth.getCradNo())) {
            return ServiceResponse.createErrorResponse("身份证格式不正确");
        }
        userAuth.setUserId(getUser().getId());
        long row = userAuthService.autoUserAuth(userAuth);
        if (row > 0) {
            return ServiceResponse.createSuccessResponse(row);
        } else {
            return ServiceResponse.createErrorResponse("认证失败");
        }
    }


    @GetMapping("/v1/userAuth/{userAuthId}")
    public ServiceResponse getUserAuth(@PathVariable Long userAuthId) {
        UserAuth userAuth = userAuthService.getUserAuth(userAuthId);
        if (userAuth == null) {
            return ServiceResponse.createErrorResponse("实名认证不存在");
        } else {
            return ServiceResponse.createSuccessResponse(userAuth);
        }
    }
}
